package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ApUserInfoMapper;
import com.zhentao.model.common.app.dto.LoginDto;
import com.zhentao.model.common.app.dto.MaAppUserFindDto;
import com.zhentao.model.common.app.dto.RealNameDto;
import com.zhentao.model.common.app.pojo.ApUser;
import com.zhentao.model.common.app.pojo.ApUserInfo;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.ApUserService;
import com.zhentao.mapper.ApUserMapper;
import com.zhentao.until.RealNameAuthenticationUntil;
import com.zhentao.utils.common.AppJwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author lenovo
* @description 针对表【ap_user】的数据库操作Service实现
* @createDate 2024-09-26 10:27:56
*/
@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser>
    implements ApUserService{
    @Resource
    private ApUserMapper apUserMapper;
    @Autowired
    private ApUserInfoMapper apUserInfoMapper;

    @Override
    public ResponseResult login(LoginDto loginDto) {
        //参数校验
        if(StringUtils.isEmpty(loginDto)){
            return ResponseResult.errorResult(400,"参数不能为空");
        }
        //查询用户是否存在
        ApUser apUser = apUserMapper.selectOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone, loginDto.getPhone()));
        if(apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //密码校验
        String salt = apUser.getSalt();
        String password = loginDto.getPassword();
        String s = DigestUtils.md5DigestAsHex((salt + password).getBytes());
        if(!s.equals(apUser.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //生成token
        Map<String, Object> map = new HashMap<>();
        apUser.setSalt("");
        apUser.setPassword("");
        map.put("user",apUser);
        map.put("token", AppJwtUtil.getToken(apUser.getId().longValue()));
        return ResponseResult.okResult(map);
    }

    /**
     * app端：实名认证功能
     * @param dto
     * @return
     */
    @Override
    public ResponseResult realName(RealNameDto dto) {
        //判断参数是否等于空
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //调用阿里云的身份杨正接口
        ResponseResult result = RealNameAuthenticationUntil.RealNameAuthentication(dto.getName(), dto.getCard());
        if (result.getCode()!=200){
            return ResponseResult.errorResult(801,"身份不匹配");
        }
        //将真实姓名和身份证信息存入用户信息表里面
        ApUser apUser = apUserMapper.selectById(dto.getUserId());
        apUser.setRelaNameStatus(2);
        apUserMapper.updateById(apUser);
        //查询用户信息表
        QueryWrapper<ApUserInfo>wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(ApUserInfo::getUid, dto.getUserId());
        ApUserInfo apUserInfo = apUserInfoMapper.selectOne(wrapper);
        apUserInfo.setCarid(dto.getCard());
        apUserInfo.setName(dto.getName());
        apUserInfoMapper.updateById(apUserInfo);
        return ResponseResult.okResult("实名认证成功");
    }


    /**
     * 后台管理：app用户列表查询
     * @return
     */
    @Override
    public ResponseResult findUser(MaAppUserFindDto dto) {
        Page<ApUser> page=new Page(dto.getPage(),dto.getSize());
        QueryWrapper<ApUser>wrapper=new QueryWrapper<>();
        wrapper.lambda().eq(dto.getVid()!=null, ApUser::getVid, dto.getVid());
        wrapper.lambda().eq(dto.getRealNameStatus()!=null, ApUser::getRelaNameStatus, dto.getRealNameStatus());
        Page<ApUser> page1 = apUserMapper.selectPage(page, wrapper);
        System.out.println(page1);
        return ResponseResult.okResult(page1);
    }

    @Override
    public ResponseResult updateStatus(Integer id, Integer status) {
        ApUser apUser = apUserMapper.selectById(id);
        if(apUser.getStatus()==1){
            apUser.setStatus(2);
            apUserMapper.updateById(apUser);
        } else if (apUser.getStatus()==2) {
            apUser.setStatus(1);
            apUserMapper.updateById(apUser);
        }
        return ResponseResult.okResult("操作成功");
    }
}




